package org.funtester.performance.books.chapter08.section2;

import com.funtester.fungrpc.QueryRequest;
import com.funtester.fungrpc.QueryResponse;
import com.funtester.fungrpc.UserServiceGrpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.stub.StreamObserver;
import org.funtester.performance.books.chapter03.common.ThreadTool;

public class StreamClient {

    public static void main(String[] args) {
        ManagedChannel managedChannel = ManagedChannelBuilder// 创建连接builder对象
                .forAddress("127.0.0.1", 8080)// 指定地址和端口
                .usePlaintext()// 使用明文传输,不使用ssl安全连接
                .build();// 创建连接

        UserServiceGrpc.UserServiceStub userServiceStub = UserServiceGrpc.newStub(managedChannel);// 创建流式存根
        QueryRequest queryRequest = QueryRequest.newBuilder().setId(888).build();// 创建请求对象
        StreamObserver streamObserver = new StreamObserver<QueryResponse>() {// 创建响应观察者
            @Override
            public void onNext(QueryResponse o) {// 收到响应时调用
                System.out.println(o.getName());
            }

            @Override
            public void onError(Throwable throwable) {// 出错时调用
                System.out.println(throwable.getMessage());
            }

            @Override
            public void onCompleted() {// 完成时调用
                System.out.println("完成");
            }
        };
        userServiceStub.queryUser(queryRequest, streamObserver);// 发送请求
        ThreadTool.sleep(300);
    }

}
